import org.apache.spark.SparkConf
import org.apache.spark.streaming.{Seconds, StreamingContext}
import org.apache.spark.streaming.dstream.{DStream, ReceiverInputDStream}

/**
 * SparkStreaming scala word count
 */
object wcountt {
  def main(args: Array[String]): Unit = {

    //1. 配置文件
    val sparkConf: SparkConf = new SparkConf().setAppName("wc").setMaster("local[2]")
    //2.构建ssc
    val ssc: StreamingContext = new StreamingContext(sparkConf, Seconds(5))
    //3.输入数据 --source
    val stream: ReceiverInputDStream[String] = ssc.socketTextStream("gpu116", 9999)
    //4.数据处理  --transformation
    val resutlStream: DStream[(String, Int)] = stream.flatMap(_.split(" ")).map((_, 1)).reduceByKey(_ + _)
    //5.结果保存或者打印  --sink
    resutlStream.print()
    val value: DStream[(String, Int)] = resutlStream.window(Seconds(10), Seconds(5))

    //6.启动Spark流计算
    ssc.start()
    ssc.awaitTermination()
  }
}
